<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="交易账号" prop="tdAcc">
        <el-input v-model="queryParams.tdAcc" placeholder="请输入交易账号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="总额度" prop="alimit">
        <el-input v-model="queryParams.alimit" placeholder="请输入总额度" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['business:tradefinancing:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['business:tradefinancing:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['business:tradefinancing:remove']">删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['business:tradefinancing:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tradefinancingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="交易账号" align="center" prop="tdAcc" />
      <el-table-column label="总额度" align="center" prop="alimit" />
      <el-table-column label="日费率" align="center" prop="rate" :formatter="rateFormatter" />
      <el-table-column label="已用额度" align="center" prop="yyEd" />
      <el-table-column label="总利息" align="center" prop="zlxJe" />
      <el-table-column label="已还利息" align="center" prop="hxJe" />
      <el-table-column label="已还本金" align="center" prop="hbJe" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <!-- <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['business:tradefinancing:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['business:tradefinancing:remove']">删除</el-button>
        </template>  -->
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="repayment(scope.row)"
            v-hasPermi="['business:tradefinancing:edit']">还本</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleInterest(scope.row)"
            v-hasPermi="['business:tradefinancing:remove']">还息</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改账号融资对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="交易账号" prop="tdAcc">
              <el-input v-model="form.tdAcc" placeholder="请输入交易账号" />
            </el-form-item>
          </el-col> <el-col :span="12">
            <el-form-item label="总额度" prop="alimit">
              <el-input v-model="form.alimit" placeholder="请输入总额度" />
            </el-form-item></el-col><el-col :span="12">
            <el-form-item label="日费率" prop="rate">
              <el-input v-model="form.rate" placeholder="请输入日费率" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="已用额度" prop="yyEd">
              <el-input v-model="form.yyEd" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="open2" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="交易账号" prop="tdAcc">
              <el-input v-model="form.tdAcc" placeholder="请输入交易账号" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="总额度" prop="alimit">
              <el-input v-model="form.alimit" placeholder="请输入总额度" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="已用额度" prop="yyEd">
              <el-input v-model="form.yyEd" placeholder="请输入已用额度" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="还本金额" prop="thisRepayment">
              <el-input v-model="form.thisRepayment" placeholder="请输入还款金额" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="已还金额" prop="hbJe">
              <el-input v-model="form.hbJe" placeholder="已还金额" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitHbje">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="open3" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="交易账号" prop="tdAcc">
              <el-input v-model="form.tdAcc" placeholder="请输入交易账号" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="总额度" prop="alimit">
              <el-input v-model="form.alimit" placeholder="请输入总额度" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="已用额度" prop="yyEd">
              <el-input v-model="form.yyEd" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="还息金额" prop="thisInterest">
              <el-input v-model="form.thisInterest" placeholder="请输入还息金额" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-col :span="12">
          <el-form-item label="总利息" prop="zlxJe">
            <el-input v-model="form.zlxJe" placeholder="总利息" :disabled="true" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="已还利息" prop="zlxJe">
            <el-input v-model="form.hxJe" placeholder="已还利息" :disabled="true" />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitHxje">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTradefinancing, repaymentofprincipal, payInterest, getTradefinancing, delTradefinancing, addTradefinancing, updateTradefinancing } from "@/api/business/tradefinancing";

export default {
  name: "Tradefinancing",
  data() {
    return {
      // 表单参数
      form: {
        yyEd: '0',
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 账号融资表格数据
      tradefinancingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      open3: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tdAcc: null,
        alimit: null,
      },
      // 表单校验
      rules: {
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "修改时间不能为空", trigger: "blur" }
        ],
        // remarks: [
        //   { required: true, message: "备注不能为空", trigger: "blur" }
        // ],
        tdAcc: [
          { required: true, message: "交易账号不能为空", trigger: "blur" }
        ],
        alimit: [
          { required: true, message: "总额度不能为空", trigger: "blur" }
        ],
        rate: [
          { required: true, message: "月费率不能为空", trigger: "blur" }
        ],
        // yyEd: [
        //   { required: true, message: "已用额度不能为空", trigger: "blur" }
        // ],
        hbJe: [
          { required: true, message: "还本金额不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询账号融资列表 */
    getList() {
      this.loading = true;
      listTradefinancing(this.queryParams).then(response => {
        this.tradefinancingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    rateFormatter(row) {
      // 保证为空时不报错
      return row.rate !== undefined && row.rate !== null ? row.rate + '%' : '';
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open2 = false;
      this.open3 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createTime: null,
        updateTime: null,
        remarks: null,
        tdAcc: null,
        alimit: null,
        rate: null,
        yyEd: null,
        hbJe: null,
        hxJe: null,
        zlxJe: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /**还息 */
    handleInterest(row) {
      this.reset();
      const id = row.id || this.ids
      getTradefinancing(id).then(response => {
        this.form = response.data;
        this.form.thisInterest = Number(this.form.zlxJe) - Number(this.form.hxJe)
        this.open3 = true;
        this.title = "还息";
      });
    },
    submitHxje() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (Number(this.form.thisInterest) + Number(this.form.hxJe) > Number(this.form.zlxJe)) {
            let a = Number(this.form.zlxJe) - Number(this.form.hxJe)
            return this.$modal.msgError("本次还息金额最大为[" + a + "]");
          }
          if (this.form.id != null) {
            payInterest(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open2 = false;
              this.getList();
            });
          }
        }
      });
    },
    /**还本 */
    repayment(row) {
      this.reset();
      const id = row.id || this.ids
      getTradefinancing(id).then(response => {
        this.form = response.data;
        this.form.thisRepayment = Number(this.form.alimit) - Number(this.form.hbJe)
        console.log(this.form.thisRepayment)
        this.open2 = true;
        this.title = "还本";
      });
    },
    submitHbje() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (Number(this.form.thisRepayment) + Number(this.form.hbJe) > Number(this.form.alimit)) {
            let a = Number(this.form.alimit) - Number(this.form.hbJe)
            return this.$modal.msgError("本次还本金额最大为[" + a + "]");
          }
          debugger
          if (this.form.id != null) {
            repaymentofprincipal(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open2 = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      this.open = true;
      this.title = "添加账号融资";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTradefinancing(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改账号融资";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTradefinancing(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTradefinancing(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除账号融资编号为"' + ids + '"的数据项？').then(function () {
        return delTradefinancing(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/tradefinancing/export', {
        ...this.queryParams
      }, `tradefinancing_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
